import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_bsbdj/bean/music_channel_gg.dart';
import 'package:flutter_bsbdj/pagers/music/music_channel_man_detail.dart';
import 'package:flutter_bsbdj/pagers/music/music_suspension_window.dart';
import 'package:flutter_bsbdj/utils/widget_views.dart';

//电台详情列表//公共频道
class ChannelGGDetail extends StatefulWidget {
  String name;
  String ch_name;

  ChannelGGDetail(this.name, this.ch_name);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return DetailWidget();
  }
}

class DetailWidget extends State<ChannelGGDetail> {
  String url_cid_1 =
      "https://api.apiopen.top/musicBroadcastingDetails?channelname=";

  List<ChannelItem> _channels;
  bool loading = true;
  double _marginBottom = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getChannelDetail();
    _changMarginBottom();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.name),
      ),
      body: Container(
        child: loading
            ? WidgetViews.getInstance().loading()
            : Container(
                child: (_channels == null || _channels.length == 0)
                    ? WidgetViews.getInstance().nullDataPormat(() {
                        _getChannelDetail();
                      })
                    : ListView.builder(
                        itemCount: _channels.length,
                        itemBuilder: (content, index) {
                          ChannelItem item = _channels[index];
                          return item.title == null
                              ? Container()
                              : GestureDetector(
                                  child: Container(
                                    height: 60,
                                    color: Colors.white,
                                    margin: EdgeInsets.all(5),
                                    padding: EdgeInsets.all(5),
                                    child: Row(
                                      children: <Widget>[
                                        Container(
                                          width: 50,
                                          height: 50,
                                          margin: EdgeInsets.only(right: 8),
                                          child: WidgetViews.getInstance()
                                              .cachedImageCover(item.thumb),
                                        ),
                                        Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(
                                              item.title == null
                                                  ? ""
                                                  : item.title,
                                              style: TextStyle(
                                                  fontSize: 15,
                                                  color: Colors.blueAccent),
                                            ),
                                            Expanded(
                                              child: Container(
                                                alignment: Alignment.bottomLeft,
                                                child: Text(
                                                  item.artist == null
                                                      ? ""
                                                      : item.artist,
                                                  style: TextStyle(
                                                      color: Colors.grey),
                                                ),
                                              ),
                                            )
                                          ],
                                        )
                                      ],
                                    ),
                                  ),
                                  onTap: () {
                                    Navigator.of(context).push(
                                        MaterialPageRoute(
                                            builder: (context) =>
                                                ChannelManDetail(
                                                    item.title, item.title)));
                                  },
                                );
                        }),
              ),
      ),
    );
  }

  void _getChannelDetail() async {
    var t = await Dio().get("$url_cid_1${widget.ch_name}").then((value) {
      try {
        print(value);
        MusicChannelGg channelGg = MusicChannelGg.fromJson(value.data);
        //只有当前widget还在 tree中才设置值
        if (mounted) {
          setState(() {
            loading = false;
            _channels = channelGg.result.songlist;
          });
        }
      } catch (e) {}
    });
  }

  void _changMarginBottom() async {
    await Future.delayed(Duration(milliseconds: 400), () {
      setState(() {
        if (MusicWindowPlay.getInstance(context).musicIsPlaying()) {
          _marginBottom = 60;
          MusicWindowPlay.getInstance(context).isHeidden(false);
        } else {
          _marginBottom = 0;
        }
      });
    });
  }
}
